package com.xiaodeng.edu.service.impl;

import cn.hutool.core.util.StrUtil;
import com.xiaodeng.edu.exception.UserException;
import com.xiaodeng.edu.mapper.UserMapper;
import com.xiaodeng.edu.pojo.User;
import com.xiaodeng.edu.service.UserService;
import com.xiaodeng.edu.utils.JwtUtil;
import com.xiaodeng.edu.utils.Md5Util;
import com.xiaodeng.edu.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    //用户名查询用户
    @Override
    public User findByUserName(String username) {
        return userMapper.findByUserName(username);
    }


    //用户注册
    @Override
    public void register(String username, String password) {
        //用户名查询用户
        User u = this.findByUserName(username);
        if(u != null){
            throw new UserException(401,"该用户已存在");
        }
        //加密处理
        String md5String = Md5Util.getMD5String(password);
        //注册
        userMapper.add(username,md5String);
    }

    //用户登录
    @Override
    public String login(String username, String password) {
        //用户名查询用户

        User loginuser = this.findByUserName(username);
        System.out.println(loginuser.getUsername());
        if(loginuser == null){
            throw new UserException(401,"用户不存在");
        }
        if (!loginuser.getPassword().equals(Md5Util.getMD5String(password))){
            throw new UserException(402,"账号或者密码错误");
        }
        Map<String,Object> claims = new HashMap<>();
        claims.put("id",loginuser.getId());
        claims.put("username",loginuser.getUsername());
        String token = JwtUtil.genToken(claims);
        return token;
    }

    //更新用户头像
    @Override
    public void updateAvatar(String avatarUrl) {
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        userMapper.updateAvatar(avatarUrl,id);
    }

    //删除用户
    @Override
    public void delete(Integer id) {
        userMapper.delete(id);
    }

    //更新用户信息
    @Override
    public void update(User user) {
        userMapper.update(user);
    }

    //用户修改密码
    @Override
    public void updatePwd(String newPwd, User user) {

        //根据用户名拿到原密码跟新密码对比
        Map<String,Object> map = ThreadLocalUtil.get();
        String username = (String) map.get("username");
        Integer id =(Integer) map.get("id");
        System.out.println(username);
        User loginUser = this.findByUserName(username);
        if (StrUtil.isBlank(user.getPassword()) ){
            throw new UserException(401,"原密码为空");
        }
        if (StrUtil.isBlank(loginUser.getPassword())){
            throw new UserException(401,"新密码为空");
        }
        if (!loginUser.getPassword().equals(Md5Util.getMD5String(user.getPassword()))){
            throw new UserException(401,"请输入正确的原密码");
        }
        userMapper.updatePwd(Md5Util.getMD5String(newPwd), id);

    }


}
